<?php

namespace app\index\controller;

use app\BaseController;
use app\index\validate\Link as ValidateLink;
use think\exception\ValidateException;
use think\facade\Db;

class Link extends BaseController
{
    public function Index()
    {
        $list = Db::name('link')->where('status',1)->order('id , sort')->select();
        return view('index',['list' => $list]);
    }
    public function apply()
    {
        return view();
    }
 
    public function doApply()
    {
        if (request()->isPost()) {
            $param = input();
            if (!captcha_check($param['captcha']))  return error('验证码错误');
            try {
                validate(ValidateLink::class)->check($param);
                $hasHref = Db::name('link')->where('web_href', 'like', '%' . $param['web_href'] . '%')->find();
                if ($hasHref) return error('无需重复提交哈，感谢你的支持！');
                $data = [
                    'web_name' => $param['web_name'],
                    'web_description' => $param['web_description'],
                    'web_href' => $param['web_href'],
                    'create_time' => time(),
                    'status' => 0
                ];
                $res = Db::name('link')->insert($data);
                if ($res) return success();
            } catch (ValidateException $e) {
                return error($e->getError());
            }
            return error();
        }
    }
}
